package com.yanq.liancar.fragment;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.vondear.rxtool.RxActivityTool;
import com.vondear.rxtool.view.RxToast;
import com.yanq.liancar.R;
import com.yanq.liancar.activity.OrderDetailsActivity;
import com.yanq.liancar.activity.OrderPaymentActivity;
import com.yanq.liancar.activity.ShopEvaluationActivity;
import com.yanq.liancar.activity.SmallBOrderDetailsActivity;
import com.yanq.liancar.adapter.MyServiceOrderAdapter;
import com.yanq.liancar.base.BaseLYFragment;
import com.yanq.liancar.bean.ServiceOrderBean;
import com.yanq.liancar.bean.UserBean;
import com.yanq.liancar.url.Config;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class CServiceOrderFragment extends BaseLYFragment {

    @BindView(R.id.order_recyclerview)
    RecyclerView mRecyclerview;
    @BindView(R.id.smartrefreshlayout)
    SmartRefreshLayout mSmartRefreshLayout;
    @BindView(R.id.ll_no_order)
    LinearLayout llNoOrder;
    private boolean isClear = true;
    private int page = 1;
    private int type;
    private MyServiceOrderAdapter serviceOrderAdapter;
    private List<ServiceOrderBean.DataBean> myServiceOrderBeanList = new ArrayList<>();

    public static CServiceOrderFragment newInstance(int auditStatus, String where) {
        Bundle bundle = new Bundle();
        bundle.putInt("auditStatus", auditStatus);
        bundle.putString("where", where);
        CServiceOrderFragment pageFragment = new CServiceOrderFragment();
        pageFragment.setArguments(bundle);
        return pageFragment;
    }

    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_serviceorder);
        type = getArguments().getInt("auditStatus");

        initRecyclerview();
        initListener();

    }

    private void initRecyclerview() {

        mRecyclerview.setNestedScrollingEnabled(false);
        mRecyclerview.setLayoutManager(new LinearLayoutManager(mActivity));
        serviceOrderAdapter = new MyServiceOrderAdapter(R.layout.item_myserviceorder, myServiceOrderBeanList);
        mRecyclerview.setAdapter(serviceOrderAdapter);
        serviceOrderAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("onum", myServiceOrderBeanList.get(position).getOrder().getOnum());
                RxActivityTool.skipActivity(mActivity, OrderDetailsActivity.class, bundle);
            }
        });
        serviceOrderAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                if (myServiceOrderBeanList.get(position).getOrder().getOrderStatus() == 1) {       //去支付
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("data", myServiceOrderBeanList.get(position));
                    readyGo(OrderPaymentActivity.class, bundle);
                } else if (myServiceOrderBeanList.get(position).getOrder().getOrderStatus() == 3) {     //查看拒单原因
                    serviceDialog(myServiceOrderBeanList.get(position).getOrder().getRejectReason());
                } else if (myServiceOrderBeanList.get(position).getOrder().getOrderStatus() == 5) {     //评价
                    Bundle b = new Bundle();
                    b.putString("orderId", myServiceOrderBeanList.get(position).getOrder().getOnum());
                    RxActivityTool.skipActivity(mActivity, ShopEvaluationActivity.class, b);
                }
            }
        });
    }

    private void initListener() {
        mSmartRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                isClear = true;
                initOkGoWhole();

                refreshlayout.finishRefresh();
            }
        });
        mSmartRefreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                isClear = false;
                initOkGoWhole();
                refreshLayout.finishLoadMore();
            }

        });
    }

    //全部
    private void initOkGoWhole() {
        if (type == 0) {
            OkGo.<String>post(Config.s + "llc/carServiceOrderController/queryCarServiceOrder")
                    .tag(this)
                    .params("token", UserBean.getcToken(mActivity))
                    .params("status", type)
                    .params("page", page)
                    .params("num", 6)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            ServiceOrderBean bean = gson.fromJson(response.body(), ServiceOrderBean.class);
                            if (bean.isIsSuccess()) {
                                if (isClear) {
                                    myServiceOrderBeanList.clear();
                                }
                                myServiceOrderBeanList.addAll(bean.getData());
                                serviceOrderAdapter.setNewData(myServiceOrderBeanList);
                                if (myServiceOrderBeanList.size() == 0) {
                                    llNoOrder.setVisibility(View.VISIBLE);
                                } else {
                                    llNoOrder.setVisibility(View.GONE);
                                }
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            RxToast.error("服务器连接失败");
                            super.onError(response);
                        }
                    });
        } else {
            OkGo.<String>post(Config.s + "llc/carServiceOrderController/queryCarServiceOrderByStatus")
                    .tag(this)
                    .params("token", UserBean.getcToken(mActivity))
                    .params("status", type)
                    .params("page", page)
                    .params("num", 6)
                    .execute(new StringCallback() {
                        @Override
                        public void onSuccess(Response<String> response) {
                            Gson gson = new Gson();
                            ServiceOrderBean bean = gson.fromJson(response.body(), ServiceOrderBean.class);
                            if (bean.isIsSuccess()) {
                                if (isClear) {
                                    myServiceOrderBeanList.clear();
                                }
                                myServiceOrderBeanList.addAll(bean.getData());
                                serviceOrderAdapter.setNewData(myServiceOrderBeanList);
                                if (myServiceOrderBeanList.size() == 0) {
                                    llNoOrder.setVisibility(View.VISIBLE);
                                } else {
                                    llNoOrder.setVisibility(View.GONE);
                                }
                            } else {
                                RxToast.error(bean.getOutputMessage());
                            }
                        }

                        @Override
                        public void onError(Response<String> response) {
                            RxToast.error("服务器连接失败");
                            super.onError(response);
                        }
                    });
        }


    }

    @Override
    protected void onResumeLazy() {
        super.onResumeLazy();
        initOkGoWhole();
    }

    private void serviceDialog(String str) {
        final Dialog bottomDialog = new Dialog(mActivity);
        View contentView = LayoutInflater.from(mActivity).inflate(R.layout.refuse_reason, null);
        bottomDialog.setContentView(contentView);
        TextView tv_yuanyin = contentView.findViewById(R.id.tv_yuanyin);
        tv_yuanyin.setText(str);
        ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) contentView.getLayoutParams();
        params.width = getResources().getDisplayMetrics().widthPixels - 320;
        params.bottomMargin = 40;
        contentView.setLayoutParams(params);
        bottomDialog.getWindow().setGravity(Gravity.CENTER);
        bottomDialog.getWindow().setWindowAnimations(R.style.BottomDialog_Animation2);
        bottomDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
        bottomDialog.show();
    }
}
